//
//  RJLoadView.m
//  AirQuality
//
//  Created by Po on 15/3/19.
//  Copyright (c) 2015年 Po. All rights reserved.
//

#import "RJLoadView.h"
@interface RJLoadView ()

@property (retain, nonatomic) UIImageView * background;

@end
@implementation RJLoadView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
//        [self setBackgroundColor:[UIColor colorWithWhite:0.3 alpha:0.5]];
        [self addSubview:[self getBackground]];
        [self addSubview:[self getLoadingLabel]];
    }
    return self;
}

- (UIImageView *)getBackground
{
    if (!_background) {
        _background = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 40, 40)];
        [_background setCenter:CGPointMake(self.frame.size.width / 2, self.frame.size.height / 2)];
        NSMutableArray * animationImages = [NSMutableArray array];
        for (int i = 1; i < 9; i ++) {
            [animationImages addObject:[UIImage imageNamed:[NSString stringWithFormat:@"load_0%d",i]]];
        }
        _background.animationImages = animationImages;
        [_background setAnimationDuration:1.0];
        [_background setAnimationRepeatCount:0];
        [_background startAnimating];
    }
    return _background;
}

- (UILabel *)getLoadingLabel
{
    UILabel * label = [[UILabel alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(_background.frame) + 10, self.frame.size.width, 30)];
    [label setFont:[UIFont boldSystemFontOfSize:20]];
    [label setText:@"正在努力加载..."];
    [label setTextAlignment:NSTextAlignmentCenter];
    [label setTextColor:[UIColor whiteColor]];
    [label.layer setShadowOffset:CGSizeMake(0, 2)];
    [label.layer setShadowOpacity:0.8];
    return label;
}

- (void)removeSelf
{
    //停止GIF动画
    [_background stopAnimating];
    //移除自身视图
    [self removeFromSuperview];
}
@end
